package org.sandbox2.domain.loan;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.roo.addon.entity.RooEntity;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.tostring.RooToString;

@RooJavaBean
@RooToString
@RooEntity
public class LoanRepository {

	static List<LoanBalanceForm> findBalanceAt(Long borrowerId, Date paymentDate) {

		System.out.println("LoanRepository#findBalanceAt");
		System.out.println("borrwerId : " + borrowerId);
		System.out.println("paymentDate : " + paymentDate);

		List<LoanBalanceForm> result = new ArrayList<LoanBalanceForm>();
		List<Loan> loans = Loan.findLoansByBorrowerId(borrowerId)
				.getResultList();

		// System.out.println("loans.size() : " + loans.size());

		for (Loan loan : loans) {
			for (PaymentDetail detail : loan.paymentDetails) {
				// if (detail.getPaymentTime() == 1) {
				if (detail.getPaymentDate().toGMTString()
						.equals(paymentDate.toGMTString())) {
					LoanBalanceForm form = new LoanBalanceForm();
					form.setBorrowerId(borrowerId);
					form.setLoanId(loan.getId());
					form.setBalance(detail.getBalanceOfPrincipal().intValue());
					form.setPaymentDate(paymentDate);
					result.add(form);
					System.out.println(form);
				}
			}
		}

		// System.out.println("result.size() : " + result.size());

		return result;
	}
}
